import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import java.util.List;
import java.util.logging.Level;
import javax.accessibility.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.rsbot.bot.Bot;
import org.rsbot.script.*;
import org.rsbot.script.wrappers.*;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.accessors.*;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.event.listeners.ServerMessageListener;
import org.rsbot.event.events.ServerMessageEvent;
import org.rsbot.util.ScreenshotUtil;

@ScriptManifest(authors = { "EricTao" }, category = "Mining", name = "AlkharidSmitherMiner", version = 0.9, description = "<html><head></head><body>Mines coal and iron, then goes to smelt and bank them. <br> <b>WARNING: do not equip pickaxe</b> <br><br> BETA RELEASE 900 <br> Author: Ericthecm </body></html\n")
public class AlkharidSmitherMiner extends Script implements PaintListener, ServerMessageListener {
	int[] furnaceid = {11666,26814};
	boolean maybegin = false;
	int smithed = 0;
	public boolean onStart(Map<String, String> args) {
		if(isLoggedIn()){
			wait(2000);
			int pickaxes = getInventoryCount(1275);
			if(pickaxes == 0){
				while(!inArea(3269,3164,3272,3169)){
					walkThere(new RSTile(3271 + (int)(Math.random()*3.0 - 1),3166 + (int)(Math.random()*3.0 - 1)));
				}
				RSObject bankbooth = getNearestObjectByID(35647);
				if(bankbooth != null){
					atObject(bankbooth, "Use-quickly");
					while(!bank.isOpen()){
						wait(2000);
					}
					bank.withdraw(1275, 1);
				}
			}
		}
		return true;
	}
	public boolean inArea(int xmin, int ymin, int xmax, int ymax){
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		if(x <= xmax && x >= xmin && y <= ymax && y>= ymin){
			return true;
		}else{
			return false;
		}
	}
	final private RSTile[] toMine = {new RSTile(3276,3173),new RSTile(3280,3182),new RSTile(3280,3195),new RSTile(3279,3207),new RSTile(3278,3219),new RSTile(3278,3229),new RSTile(3278,3239),new RSTile(3279,3249),new RSTile(3284,3259),new RSTile(3291,3268),new RSTile(3294,3275),new RSTile(3298,3285),new RSTile(3298,3294),new RSTile(3300,3300)};
	final private RSTile[] fromMine = {new RSTile(3298,3294),new RSTile(3298,3285),new RSTile(3294,3275),new RSTile(3291,3268),new RSTile(3284,3259),new RSTile(3279,3249),new RSTile(3278,3239),new RSTile(3278,3229),new RSTile(3278,3219),new RSTile(3279,3207),new RSTile(3281,3197),new RSTile(3276,3186)};
	final private RSTile[] toBank = {new RSTile(3276,3177), new RSTile(3270,3170)};
	final private int[] ironores = {37307,37309};
	final private int[] coalores = {11930,11932};
	public int loop() {
		if(isLoggedIn()){
			if(getInventoryCount(440) < 9 && !inArea(3274,3185,3279,3188)){
				if(!inArea(3296,3297,3303,3303))
					walk(toMine);
				if(getMyPlayer().getAnimation()==-1){
					wait(500);
					if(getMyPlayer().getAnimation()==-1){
						wait(500);
						if(getMyPlayer().getAnimation()==-1){
							wait(500);
							if(getMyPlayer().getAnimation()==-1){
								RSObject iron = getNearestObjectByID(ironores);
								if(iron!=null&&distanceTo(iron.getLocation()) < 5){
									atObject(iron, "Mine");
								}
							}
						}
					}
				}
			}else if(getInventoryCount(453) < 18 && !inArea(3274,3185,3279,3188)){
				if(getMyPlayer().getAnimation()==-1){
					wait(500);
					if(getMyPlayer().getAnimation()==-1){
						wait(500);
						if(getMyPlayer().getAnimation()==-1){
							wait(500);
							if(getMyPlayer().getAnimation()==-1){
								RSObject coal = getNearestObjectByID(coalores);
								if(coal!=null){
									atObject(coal, "Mine");
								}
							}
						}
					}
				}
			}else if(isInventoryFull()&&getInventoryCount(2353)==0){
				log(getInventoryCount(2353)+"");
				if(!inArea(3274,3185,3279,3188))
					walk(fromMine);
				wait(1000);
				RSObject furnace = getNearestObjectByID(11666);
				Point location = Calculations.tileToScreen(furnace.getLocation());
				moveMouse(location,2,2);
				wait(500);
				clickMouse(true);
				wait(1000);
				moveMouse(new Point(257,413), 3, 3);
				wait(500);
				clickMouse(false);
				wait(1000);
				moveMouse(new Point(284, 464), 2, 2);
				wait(500);
				clickMouse(true);
				wait(3000);
			}else if(getInventoryCount(2353) == 9){
				log("OK");
				log(getInventoryCount(2353)+"");
				if(!inArea(3269,3166,3271,3172))
					walk(toBank);
				wait(1000);
				RSObject bankbooth = getNearestObjectByID(35647);
				if(bankbooth!=null){
					atObject(bankbooth, "Use-quickly");
				}
				wait(1000);
				while(!bank.isOpen()){
				}
				bank.depositAllExcept(1275);
			}
			dropGems();
			if(getInventoryCount(440)>9){
				atInventoryItem(440, "Drop");
			}
			if(getInventoryCount(453)>18){
				atInventoryItem(453, "Drop");
			}
		}
		return 100;
	}
	private void walk(RSTile[] a){
		for(int i = 0; i < a.length; i ++){
			while(!inArea(a[i].getX()-3,a[i].getY()-3,a[i].getX()+3,a[i].getY()+3)){
				if (runCheck()) {
					setRun(true);
					wait(random(450, 950));
				}
				if(distanceTo(getDestination()) < 3)
					walkTo(new RSTile(a[i].getX() + (int)(Math.random()*3 - 1), a[i].getY() + (int)(Math.random()*3 - 1)));
			}
		}
	}
	private void dropGems() {
		atInventoryItem(1623, "Drop");
		atInventoryItem(1619, "Drop");
		atInventoryItem(1621, "Drop");
	}
	public void serverMessageRecieved(final ServerMessageEvent arg0) {
		final String serverString = arg0.getMessage();
	}

	public void onRepaint(Graphics g) {
		if(isLoggedIn() ||  isLoginScreen()) {
			g.setColor(Color.RED);
			g.drawString("You have banked a total of: " + smithed + " bars.", 8,290);
			g.drawString("You are " + skills.getPercentToNextLevel(Skills.getStatIndex("Mining")) + "% to the next mining level with " + skills.getXPToNextLevel(Skills.getStatIndex("Mining")) + " xp left.", 8, 302);
			g.drawString("You are " + skills.getPercentToNextLevel(Skills.getStatIndex("Smithing")) + "% to the next smithing level with" + + skills.getXPToNextLevel(Skills.getStatIndex("Smithing")) + " xp left.", 8, 314);
		}
		/* TODO    Paint here */
	}

	public void onFinish() {
		ScreenshotUtil.takeScreenshot(true);
	}
	public int walkToX = 0000;
	public int walkToY = 0000;

	int runEnergy = random(15, 50);

	@Override
	public RSTile[] fixPath(final RSTile[] path) {
		final ArrayList<RSTile> newPath = new ArrayList<RSTile>();
		for (int i = 0; i < path.length - 1; i++) {
			final List<RSTile> temp1 = fixPath2(path[i], path[i + 1]);
			for (final RSTile element : temp1) {
				newPath.add(element);
			}
		}
		return newPath.toArray(new RSTile[newPath.size()]);
	}
	public boolean runCheck() {
		try {
			if (getEnergy() >= runEnergy && !isRunning()) {
				runEnergy = random(15, 50);
				return true;
			} 
			else {
				return false;
			}
		} 
		catch (final Exception e) {
			log.log(Level.SEVERE, "runCheck() error: ", e);
			return false;
		}
	}

	public boolean walkContinuous(final RSTile[] path) {
		try {
			if (runCheck()) {
				setRun(true);
				wait(random(450, 950));
			}
			RSTile loc = new RSTile(0, 0);
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) < random(5, 7)) {
				for (int a = path.length - 1; a >= 0; a--) {
					if (tileOnMap(path[a])) {
						loc = randomizeTile(path[a], 2, 2);
						if (tileOnMap(loc)) {
							walkTileMM(loc);
							wait(random(80, 180));
						}
						return true;
					}
				}
				return false;
			}
		} 
		catch (final Exception e) {
		}
		return true;
	}

	public boolean walkThere(final RSTile tile) {
		try {
			walkContinuous(fixPath(generateProperPath(tile)));
		} 
		catch (final Exception e) {
			log("What Happened?!");
		}
		if (distanceTo(tile) <= 3) {
			log("Here We Are!!");
			return true;
		}
		return false;
	}

}